package org.lzuploader.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.lzuploader.model.Constants;
import org.lzuploader.model.Localization;
import org.lzuploader.model.message.AbstractMessage;
import org.lzuploader.model.message.ErrorMessage;
import org.lzuploader.model.message.InfoMessage;

/** Tests the connection to a server and produces a status message.
 * 
 * @author Emmanuel Cron
 *
 */
public class NetTester extends Thread
{
    /** The logger of this class. */
    private Log log = LogFactory.getLog(NetTester.class);
    
    /** Localization of this class. */
    private Localization localization = Localization.getInstance();
    
    /** Server on which to connect to. */
    private String iServer;
    /** Port on which to connect to. */
    private int iPort;
    /** Path on which to test the connection. */ 
    private String iPath;
    
    /** Last message generated by this class. */
    private AbstractMessage lastMessage;
    
    /** 
     * Creates a new {@link NetTester} object.
     * @param server Server on which to connect to (ip or hostname).
     * @param port Port on which to connect to.
     * @param path Path on which to test the connection.
     */
    public NetTester(final String server, final int port, final String path)
    {
        iServer = server;
        iPort = port;
        iPath = path;
    }
    
    /** 
     * Runs the upload process.
     * This should not be called by the user. Use {@link #start} instead
     */
    @Override
    public void run()
    {
        log.trace("Trying to connect on server.");
        
        final String contents = NetConnection.readPage(iServer, iPort, iPath);
        
        if (NetConnection.hasMessage() && NetConnection.getLastMessage().isError())
        {
            log.trace("Connection failed, saving error message.");
                
            lastMessage = new ErrorMessage(
                localization.getString("DIALOG_TITLE_ERROR"),
                localization.getString("DIALOG_COULD_NOT_CONNECT")
            );
        }
        else if (!contents.equals(Constants.NET_OK))
        {
            log.trace("Connection successful, but contents does not equals OK. Saving error.");
            
            lastMessage = new ErrorMessage(
                localization.getString("DIALOG_TITLE_ERROR"),
                localization.getString("UPLOADER_SEND_" + contents)
            );
        }
        else
        {
            log.trace("Connection successful, saving info message.");
            
            lastMessage = new InfoMessage(
                localization.getString("DIALOG_TITLE_INFO"),
                localization.getString("DIALOG_TEST_SUCCESSFUL")
            );
        }
    }
    
    /** 
     * Returns wether {@link NetTester} generated a message on its last run.
     * @return <code>true</code> if the last process generated a message, <code>false</code> otherwise
     */
    public boolean hasMessage()
    {
        return lastMessage != null;
    }
    
    /** 
     * Returns the last message produced by this class.
     * @return An {@link AbstractMessage} object, or <code>null</code> if none exists.
     */
    public AbstractMessage getLastMessage()
    {
        return lastMessage;
    }
}
